<?php
import( 'de.ceus-media.framework.krypton.core.Logic' );
import( 'classes.model.Project' );
import( 'classes.model.ServerProject' );
import( 'classes.logic.Server' );
import( 'classes.logic.Project' );
//import( 'de.ceus-media.framework.krypton.core.database.StatementBuilder' );
/**
 *	Logic of Licencee, Locations and Companies.
 *	@package		mv2.logic
 *	@extends		Framework_Krypton_Core_Logic
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@author			David Seebacher <dseebacher@gmail.com>
 *	@since			13.03.2007
 *	@version		0.1
 */
/**
 *	Logic of Licencee, Locations and Companies.
 *	@package		mv2.logic
 *	@extends		Framework_Krypton_Core_Logic
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@author			David Seebacher <dseebacher@gmail.com>
 *	@since			13.03.2007
 *	@version		0.1
 */
class Logic_ServerProject extends Framework_Krypton_Core_Logic
{
	function addProjectToServer( $projectId, $serverId )
	{
		$model	= new Model_ServerProject();
		$model->focusIndex( 'project_id', $projectId );
		$model->focusIndex( 'server_id', $serverId );
		if( !$model->exists() )
		{
			$data	= array(
				'server_id'		=> $serverId,
				'project_id'	=> $projectId,
			);
			return $model->insert( $data );
		}
		return 0;
	}
	
	static function getServerData( $serverProjectId )
	{
		$data	= self::getServerProjectData( $serverProjectId );
		return Logic_Server::getServerData( $data['server_id'] );
	}
	
	static function getProjectsFromServer( $serverId, $assoc = 0 )
	{
		$model	= new Model_ServerProject();
		$list	= array();
		if( $assoc )
		{
			$model->focusIndex( 'server_id', $serverId );
			$relations	= $model->get( false );
			foreach( $relations as $relation )
			{
				if( $assoc == 1 )
					$list[$relation['project_id']]	= Logic_Project::getProjectTitle( $relation['project_id'] );
				else if( $assoc == 2 )
					$list[$relation['project_id']]	= Logic_Project::getProjectData( $relation['project_id'] );
			}
		}
		else
		{
			$model->focusIndex( 'project_id', $projectId );
			$relations	= $model->get( false );
			foreach( $relations as $relation )
				$list[]	= $relation['server_id'];
		}
		return $list;
	}
	
	static function getServersFromProject( $projectId, $assoc = 0 )
	{
		$list	= array();
		$model	= new Model_ServerProject();
		if( $assoc )
		{
			$model->focusIndex( 'project_id', $projectId );
			$relations	= $model->get( false );
			foreach( $relations as $relation )
			{
				if( $assoc == 1 )
					$list[$relation['server_id']]	= Logic_Server::getServerTitle( $relation['server_id'] );
				else if( $assoc == 2 )
					$list[$relation['server_id']]	= Logic_Server::getServerData( $relation['server_id'] );
			}
		}
		else
		{
			$model->focusIndex( 'project_id', $projectId );
			$relations	= $model->get( false );
			foreach( $relations as $relation )
				$list[]	= $relation['server_id'];
		}
		return $list;
	}
	
	static function getServerProjectList( $serverId = 0 )
	{
		$model	= new Model_ServerProject();
		if( $serverId )
		{
			$model->focusIndex( 'server_id', $serverId );
			$relations	= $model->get( false );
		}
		else
		{
			$relations	= $model->find();
		}
		return $relations;
	}
	
	static function getServerProjectData( $serverProjectId )
	{
		$model	= new Model_ServerProject( $serverProjectId );
		$data	= $model->get();
		return $data;
	}

	function removeProjectFromServer( $projectId, $serverId )
	{
		$model	= new Model_ServerProject();
		$model->focusIndex( 'project_id', $projectId );
		$model->focusIndex( 'server_id', $serverId );
		if( $model->exists() )
		{
			return $model->delete();
		}
		return 0;
	}
}
?>
